package com.vilicode.bean;

import com.vilicode.Utils.PriceUtil;
import lombok.Data;
import org.springframework.web.bind.annotation.RequestMapping;

import java.time.DateTimeException;
import java.util.*;
@Data
public class Order {
    private String oid;
    private Double ototal;
    private Integer oamount;
    private Integer ostatus;
    private Integer opaytype;
    private Integer uid;
    private String orealname;
    private String ophone;
    private String oaddress;
    private String odatetime;
    private Map<Integer,OrderItem> itemMap = new HashMap<Integer,OrderItem>();
    private List<OrderItem> itemList = new ArrayList<OrderItem>();

    public void addGoods(Book book) {
        if(itemMap.containsKey(book.getBid())) {
            OrderItem item = itemMap.get(book.getBid());
            item.setOiamount(item.getOiamount()+1);
        }else {
            OrderItem item = new OrderItem();
            item.setOiprice(book.getBprice());
            item.setOiamount(1);
            item.setBook(book);
            item.setOrder(this);
            item.setBid(book.getBid());
            itemMap.put(book.getBid(), item);
        }
        oamount++;

        ototal = PriceUtil.add(ototal,book.getBprice());
    }

    public void lessen(int bid) {
        if(itemMap.containsKey(bid)) {
            OrderItem item = itemMap.get(bid);
            item.setOiamount(item.getOiamount()-1);
            oamount--;
            ototal = PriceUtil.subtract(ototal,item.getOiprice());
            if(item.getOiamount()<=0) {
                itemMap.remove(bid);
            }
        }
    }
    public void delete(int bid)
    {
        if(itemMap.containsKey(bid)) {
            OrderItem item = itemMap.get(bid);
            ototal = PriceUtil.subtract(ototal,item.getOiamount()*item.getOiprice());
            oamount-=item.getOiamount();
            itemMap.remove(bid);
        }
    }
}
